package com.noodles.mall.mp;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 插入或更新时自动处理 createdTime 与 updatedTime
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        System.out.println("MyMetaObjectHandler: insertFill 开始");
        System.out.println("metaObject: "+metaObject);
        this.strictInsertFill(metaObject, "createdTime", DateUtil::date, Date.class);
        this.strictInsertFill(metaObject, "updatedTime", DateUtil::date, Date.class);
        System.out.println("MyMetaObjectHandler: insertFill 结束");
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        System.out.println("MyMetaObjectHandler: updateFill 开始");
        System.out.println("metaObject: "+metaObject);
        this.strictUpdateFill(metaObject, "updatedTime", DateUtil::date, Date.class);
        System.out.println("MyMetaObjectHandler: updateFill 结束");
    }
}
